package org.example.simpleboot.initializer;

import org.example.simpleboot.config.AppConfig;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.io.Serializable;

/**
 * @author Administrator
 */
public class MyWebApplicationInitializer implements WebApplicationInitializer, Serializable {
    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("go go go go 启动容器哦～～～");
        //初始化Spring的上下文
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        //注册spring的配置类
        context.register(AppConfig.class);
        context.refresh();
        //初始化DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet(DISPATCHER_SERVLET_NAME, servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
        System.out.println("bingo! i'm waiting for request!");
    }
}
